
//  ViewController.swift
//  tanlong
//
//  Created by 于文琪 on 2020/8/1.
//  Copyright © 2020 于文琪. All rights reserved.
//

import UIKit
import WebKit
import EFQRCode
import Alamofire
import AVFoundation
// 注意添加相关framework头文件
// 使用相机设备（AVCaptureDevice）功能时
import Photos
// 使用图片库功能
import AssetsLibrary
// 使用录制视频功能
import MobileCoreServices
import HRQRCodeScanTool


class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationControllerDelegate {

    var WebView:WKWebView!
    var picker : UIImagePickerController!
    
       override func viewDidLoad() {
        super.viewDidLoad()
        let MyURL=URL(string: "https://331042kp98.qicp.vip/mobile/login/index")
        let MyRequest=URLRequest(url: MyURL!)
        WebView.load(MyRequest)
        

       // let scan=ScanViewController.init()
 
       // let scanview = ScanViewController()
      //  self.present(scanview, animated: true, completion: nil)
     //  let sb = UIStoryboard(name:"Main", bundle: nil)
     //   let vc = sb.instantiateViewController(withIdentifier: "scanview") as! UIViewController
     //   self.present(vc, animated: true, completion: nil)
        
        //qrcode()
       //fromAlbum()
        //goCamera()
        //Camera()
       // showBottomAlert()

       }
    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
        let sb = UIStoryboard(name:"Main", bundle: nil)
        let vc = sb.instantiateViewController(withIdentifier: "scanview")
        self.present(vc, animated: true, completion: nil)
    }
       
       override func loadView() {
       let webconfig=WKWebViewConfiguration()
           WebView=WKWebView(frame: .zero, configuration: webconfig)
           view=WebView
       }

    
    
    func qrcode(){
    
        if let testImage = UIImage(named: "tese.png")?.cgImage {
        if let tryCodes = EFQRCode.recognize(image: testImage) {
            if tryCodes.count > 0 {
                print("There are \(tryCodes.count) codes in testImage.")
                for (index, code) in tryCodes.enumerated() {
                    print("The content of \(index) QR Code is: \(code).")
                }
            } else {
                print("There is no QR Codes in testImage.")
            }
        } else {
            print("Recognize failed, check your input image!")
        }
    }
    
    }
    
 

    

    //选取相册
        @IBAction func fromAlbum() {
            //判断设置是否支持图片库
            if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
                //初始化图片控制器
                let picker = UIImagePickerController()
                //设置代理
                picker.delegate = self
                //指定图片控制器类型
                picker.sourceType = .photoLibrary
                //弹出控制器，显示界面
                self.present(picker, animated: true, completion: {
                    () -> Void in
                })
            }else{
                print("读取相册错误")
            }
        }

 
    //选择图片成功后代理
    internal func imagePickerController(_ picker: UIImagePickerController,
                               didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
         // print(UIImagePickerController.InfoKey.originalImage.rawValue)

       // print("看看这里进来没有")
        //获取选择的原图
        let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
         //这里是选择相册里面的二维码扫描
        
        //<-这里识别二维码开始->
        //二维码读取
//        let ciImage:CIImage=CIImage(image:pickedImage)!
//        let context = CIContext(options: nil)
//        let detector = CIDetector(ofType: CIDetectorTypeQRCode, context: context,
//                                  options: [CIDetectorAccuracy:CIDetectorAccuracyHigh])
//        if let features = detector?.features(in: ciImage) {
//            print("扫描到二维码个数：\(features.count)")
//            //遍历所有的二维码，并框出
//            for feature in features as! [CIQRCodeFeature] {
//                print(feature.messageString ?? "")
//            }
//        }
        //<-这里扫描开始->
        //这里是选择相册里面的图片上传
        //<-这里上传开始->
        //将选择的图片保存到Document目录下
        let fileManager = FileManager.default
        let rootPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                                .userDomainMask, true)[0] as String
        let filePath = "\(rootPath)/pickedimage.jpg"
        let imageData = pickedImage.jpegData(compressionQuality: 1.0)
        fileManager.createFile(atPath: filePath, contents: imageData, attributes: nil)
         
        //上传图片
        if (fileManager.fileExists(atPath: filePath)){
            //取得NSURL
            let imageURL = URL(fileURLWithPath: filePath)
             print("Success: \(filePath)")
             
            //使用Alamofire上传
            Alamofire.upload(imageURL, to: "http://www.hangge.com/upload.php")
                .responseString { response in
                    print("Success: \(response.result.isSuccess)")
                    print("Response String: \(response.result.value ?? "")")
            }
        }
       //<-这里上传结束->
        //图片控制器退出
        picker.dismiss(animated: true, completion:nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
   

    
    

    
}

